

/**
 * 全局信息
 */
export class GlobalModel {
    /**
     * 服务器ip
     */
    serverIp: string = 'localhost';

    /**
     * 大厅端口
     */
    readonly lobbyPort: string = '8998';

    /**
     * 游戏端口
     */
    readonly gamePort: string = '8999';

    /**
     * 账号
     */
    private _account: string = "";
    get account(): string {
        return this._account;
    };

    /**
     * 密码
     */
    private _password: string = "";
    get password(): string {
        return this._password;
    };

    /**
     * 同意协议
     */
    private _checkAgreement: boolean;
    get checkAgreement(): boolean {
        return this._checkAgreement;
    };


    constructor() {
        this._account = localStorage.getItem('account') ?? '';
        this._password = localStorage.getItem('password') ?? '';
        this._checkAgreement = localStorage.getItem('checkAgreement') == 'true';
    }

    /**
     * 设置ip
     * @param serverIp 
     */
    setServerIp(serverIp: string) {
        this.serverIp = serverIp;
    }

    /**
     * 保存登录账号密码信息
     * @param name 
     * @param data 
     */
    saveInfo(name: 'account' | 'password' | 'checkAgreement', data: string | boolean) {
        if (name == 'account') this._account = data as string;
        if (name == 'password') this._password = data as string;
        if (name == 'checkAgreement') this._checkAgreement = data as boolean;
        localStorage.setItem(name, data.toString());
    }

    /**
     * 保存账号信息
     * @param account 
     * @param password
     */
    saveAccountInfo(account: string, password: string) {
        this.saveInfo('account', account);
        this.saveInfo('password', password);
    }

    /**
     * 清理账号密码信息
     */
    clearLoginInfo() {
        localStorage.removeItem('account');
        localStorage.removeItem('password');
        localStorage.removeItem('checkAgreement');
    }
}


